<template>
    <div>
        <div style="margin-top: 10px;">
            <FormTitle>基础信息</FormTitle>
            <el-descriptions class="fixed-width-descriptions" title="" :column="4" border>
                <el-descriptions-item label="单据编号" label-align="left" align="left"
                    :class-name="!userInfo.orderNo ? 'my-content' : ''">
                    <div>{{ ruleForm.orderNo }}</div>
                </el-descriptions-item>
                <el-descriptions-item label="制单人员" label-align="left" align="left" :class-name="'my-content'">
                    <div>{{ ruleForm.inUserName }}</div>
                </el-descriptions-item>
                <el-descriptions-item label="制单时间" label-align="left" align="left" :class-name="'my-content'">
                    <div>{{ ruleForm.inTime }}</div>
                </el-descriptions-item>
                <el-descriptions-item label="单据状态" label-align="left" align="left" :class-name="'my-content'">
                    <div>{{ruleForm.orderStatus}}</div>
                </el-descriptions-item>
                <el-descriptions-item label="结算状态" label-align="left" align="left" :class-name="'my-content'">
                    <div>{{ruleForm.settleStatus}}</div>
                </el-descriptions-item>
                <el-descriptions-item label="备注" label-align="left" align="left">
                   {{ ruleForm.remark }}
                </el-descriptions-item>
            </el-descriptions>

        </div>
        <div style="margin-top: 10px;">
            <FormTitle>药库信息</FormTitle>
            <el-descriptions class="fixed-width-descriptions" title="" :column="4" border>
                <el-descriptions-item label="入库药库" label-align="left" align="left">
                    {{ruleForm.storageName}}
                </el-descriptions-item>
                <el-descriptions-item label="供应商" label-align="left" align="left">
                     {{ruleForm.supplyName}}
                </el-descriptions-item>
                <el-descriptions-item label="联系人员" label-align="left" align="left" :class-name="'my-content'">
                    {{ ruleForm.managerName }}
                </el-descriptions-item>
                <el-descriptions-item label="联系电话" label-align="left" align="left" :class-name="'my-content'">
                    {{ ruleForm.managerPhone }}
                </el-descriptions-item>
            </el-descriptions>
        </div>
        <div style="margin-top: 10px;">
            <FormTitle>产品信息</FormTitle>
            
            <el-table :data="ruleForm.detailList" border style="width: 100%;margin-top: 10px;" height="220px"
                :header-cell-style="{ textAlign: 'center' }" :cell-style="{ textAlign: 'center' }"
                header-cell-class-name="heardClassName">
                <el-table-column type="index" width="50" />
                <el-table-column property="drugName" label="药品名称" />
                <el-table-column property="guige" label="规格" />
                <el-table-column property="mfrName" label="生产厂家" />
                <el-table-column property="drugCategory" label="分类" />
                <el-table-column property="orderQty" label="采购数量" width="90">
                    <template #default="scope">
                        {{scope.row.orderQty}}
                    </template>
                </el-table-column>
                <el-table-column property="packUnit" label="单位" width="80" />
                <el-table-column property="price" label="单价" width="80" >
                    <template #default="scope">
                        {{(scope.row.price / 100).toFixed(2)}}
                    </template>
                </el-table-column>
                <el-table-column label="金额" width="80">
                    <template #default="scope">
                        {{  (scope.row.orderQty * scope.row.price / 100).toFixed(2) }}
                    </template>
                </el-table-column>
                <el-table-column property="batchNo" label="生产批号">
                    <template #default="scope">
                        {{scope.row.batchNo}}
                        <!-- <el-input v-model="scope.row.batchNo" style="width:100%;"></el-input> -->
                    </template>
                </el-table-column>
                <el-table-column property="batchDate" label="生产日期">
                    <template #default="scope">
                        {{ scope.row.batchDate }}
                        <!-- <el-date-picker v-model="scope.row.batchDate" style="width:100%;" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                            type="date" placeholder="生产日期" /> -->
                    </template>
                </el-table-column>
                <el-table-column property="expiryDate" label="有效日期">
                    <template #default="scope">
                        {{scope.row.expiryDate}}
                        <!-- <el-date-picker v-model="scope.row.expiryDate" style="width:100%;" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                            type="date" placeholder="有效日期" /> -->
                    </template>
                </el-table-column>
            </el-table>
        </div>
        
    </div>
</template>

<script setup lang="ts">
import stores from '@/store';
import type { FormRules, ElTree } from 'element-plus';
import type { PurchaseResponse, PurchaseTable, PurchaseAdd } from '@/types/purchase'
import type { DrugInfo } from '@/types/drug'
import { getOrderPurchaseList, getOrderPurchaseDetail, addPurchase, reviewPurchase, settlePurchase, refusePurchase, } from '@/api/purchase'
import type { List as storageList } from '@/types/storage/index';
import { getStorageList } from '@/api/storage'
import { getSupplyList } from '@/api/supply'
import type { SupplyInfo } from '@/types/supply';
import { getDrugProfileList } from '@/api/drug'


const dataType = defineProps<{ dataType: Number,orderId:any, storageId: Number, }>();



//药库 
const storageData = ref<storageList[]>()
const getStorageLists = async () => {
    const res = await getStorageList({
        pageNo: 1,
        pageSize: 100000,
    })
     if(res.code != '200'){
            //ElMessage.error(res.msg);
            return
        }
    storageData.value = res.data.list
}
getStorageLists()

//供应商
const storageInfo = ref<SupplyInfo>({
    managerPhone: '',
    managerName: ''
})

 
const ruleForm = ref<PurchaseAdd>({
    remark: '',
    storageId: dataType.storageId,
    supplyId: '',
    detailList: [
    ],
})



//基本信息
const today = new Date()
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
const hours = String(today.getHours()).padStart(2, '0');
const minutes = String(today.getMinutes()).padStart(2, '0');
const seconds = String(today.getSeconds()).padStart(2, '0');

const userInfo = ref<any>(
    {
        userName: getUserInfo().empName,
        time: `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`,
    }
)



const iconStyle = computed(() => {
    const marginMap = {
        large: '8px',
        default: '6px',
        small: '4px',
    }
    return {
        marginRight: marginMap['large'] || marginMap.default,
    }
})

// 订单状态PurchaseOrderStatusEnum
const purchaseOrderStatusEnum =  ref<any>(stores.useBaseDataStore().getEnumSelect('purchaseOrderStatusEnum'))
// 结算状态SettleStatusEnum
const settleStatusEnum =  ref<any>(stores.useBaseDataStore().getEnumSelect('settleStatusEnum'))

const UnitTypeEnum = ref<any>(stores.useBaseDataStore().getDictsSelect('UNIT'))
const DrugCategoryEnum = ref<any>(stores.useBaseDataStore().getEnumSelect('drugCategoryEnum'))

const getOrderPurchaseDetailApi = async() =>{
    let res = await getOrderPurchaseDetail(dataType.orderId)
     if(res.code != '200'){
            //ElMessage.error(res.msg);
            return
        }
    for (let index = 0; index < settleStatusEnum.value.length; index++) {
        let items = settleStatusEnum.value[index];
        if(res.data.settleStatus == items.value){
            res.data.settleStatus = items.text
        }
      }

      for (let index = 0; index < purchaseOrderStatusEnum.value.length; index++) {
        let items = purchaseOrderStatusEnum.value[index];
        if(res.data.orderStatus == items.value){
            res.data.orderStatus = items.text
        }
      }

      res.data.detailList.forEach(item=>{
         for (let index = 0; index < UnitTypeEnum.value.length; index++) {
            let items = UnitTypeEnum.value[index];
            if (items.unitCode == item.minPackUnit) {
                item.minPackUnit = items.unitText
            }
            if (items.unitCode == item.packUnit) {
                item.packUnit = items.unitText
            }
        }

        if (item.drugCategory == '1') {
            item.guige = item.drugDosage + '' + item.drugDosageUnit + '*' + item.minPackQty + item.minPackUnit + '/' + item.packUnit

        }
        if (item.drugCategory == '2') {
            item.guige = item.packUnit

        }
        if (item.drugCategory == '3') {
            item.guige = (item.minPackQty || '') + (item.minPackUnit || '') + '/' + item.packUnit

        }



         for (let index2 = 0; index2 < DrugCategoryEnum.value.length; index2++) {
            let item2 = DrugCategoryEnum.value[index2];
            if (item && item.drugCategory == item2.value) {
                item.drugCategory = item2.text
            }
        }
         
      })

     

    ruleForm.value = res.data
}
getOrderPurchaseDetailApi()

 

const emit = defineEmits(['closeDrawer']);
const closeDrawer = () => {
    emit('closeDrawer');
};
</script>
<style lang="scss">
.form1-ruleForm .el-form-item {
    display: block !important;
}

.my-content {
    background: var(--el-descriptions-item-bordered-label-background);
    ;
}

.fixed-width-descriptions .is-bordered-label {
    width: 90px;
}

.fixed-width-descriptions .is-bordered-content {
    width: 188px;
}
</style>